package com.dragon.mmochat.service;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.MmochatMain;
import com.dragon.mmochat.model.MmochatChenHao;
import com.dragon.mmochat.model.attackMode.MmochatAttackModeOfNormal;
import com.dragon.mmochat.model.enumType.MmochatBossLevelType;
import com.dragon.mmochat.model.enumType.MmochatChenHaoType;
import com.dragon.mmochat.model.enumType.MmochatEquipAttributeType;
import com.dragon.mmochat.model.enumType.MmochatNpcItemType;
import com.dragon.mmochat.model.enumType.MmochatRoleBmpType;
import com.dragon.mmochat.model.enumType.MmochatTaskType;
import com.dragon.mmochat.model.map.MmochatMap;
import com.dragon.mmochat.model.map.MmochatMapGuider;
import com.dragon.mmochat.model.map.MmochatMapGuiderData;
import com.dragon.mmochat.model.object.MmochatEquipAttribute;
import com.dragon.mmochat.model.role.MmochatMonster;
import com.dragon.mmochat.model.role.MmochatMonsterFactory;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.model.role.MmochatTempNpc;
import com.dragon.mmochat.model.task.MmochatNpcBattle;
import com.dragon.mmochat.model.task.MmochatTask;
import com.dragon.mmochat.util.MmochatUtil;

//地图守护神
public class MmochatMapGuiderService {
	// 地图守护神:地图ID-->当前守护神列表
	public static Map<Integer, MmochatMapGuiderData> guiderPositions = new ConcurrentHashMap<Integer, MmochatMapGuiderData>();

	// 门派王者:地图ID-->NPC
	public static Map<Integer, MmochatMapGuiderData> familyGuiderPositions = new ConcurrentHashMap<Integer, MmochatMapGuiderData>();

	public void init() {
		if (!MmochatMain.openMergeServer) {
			// 初始化配置临时地图NPC
			for (MmochatMap map : MmochatMapService.maps.values()) {
				if (map != null) {
					MmochatMapGuider guiderInfo = map.getMapGuiderInfo();
					if (guiderInfo == null) {
						continue;
					}
					switch (guiderInfo.getType()) {
					case 地图守护神:
						setDefaultMapGuiderData(map);
						break;
					case 门派最强:
						setDefaultFamilyGuiderData(map);
						break;
					default:
						break;
					}

				}
			}
		}
	}

	// 设置默认的地图守护神数据
	public static void setDefaultMapGuiderData(MmochatMap map) {
		if (map == null) {
			return;
		}
		int mapId = map.getMapId();
		MmochatMapGuider guiderInfo = map.getMapGuiderInfo();
		if (guiderInfo == null) {
			return;
		}
		int[] xDelta = new int[] { -70, -35, 0, 35, 70 };
		int[] yDelta = new int[] { 30, 15, 0, 15, 30 };
		MmochatRoleBmpType[] bmpList = new MmochatRoleBmpType[] {
				MmochatRoleBmpType.人教女图, MmochatRoleBmpType.截教男图,
				MmochatRoleBmpType.阐教女图, MmochatRoleBmpType.人教男图,
				MmochatRoleBmpType.截教女图 };

		List<MmochatTempNpc> npcList = new ArrayList<MmochatTempNpc>();
		for (int i = 0; i < 5; i++) {
			MmochatTempNpc boss = new MmochatTempNpc(true);
			MmochatNpcService.npcs.put(boss.getNpcId(), boss);
			boss.getFunctionItems().add(MmochatNpcItemType.挑战守护神);
			boss.getFunctionItems().add(MmochatNpcItemType.挑战规则及奖励);
			boss.getFunctionItems().add(MmochatNpcItemType.守护神分布);
			boss.setLevel(guiderInfo.getMyLevel());
			boss.setLifeTime(MmochatConstant.MS_PER_DAY * 3650);
			boss.setName(map.getMapName() + "守护神");
			boss.setBmpType(bmpList[i]);
			boss.setMapId(mapId);
			boss.setX((short) (guiderInfo.getX() + xDelta[i]));
			boss.setY((short) (guiderInfo.getY() + yDelta[i]));
			boss.setFace(guiderInfo.getFace());

			map.addNpc(boss);
			npcList.add(boss);

			MmochatTask newTask = new MmochatTask();
			newTask.setTaskId(0);
			boss.addTask(newTask);
			MmochatNpcBattle battle = new MmochatNpcBattle();
			newTask.setFinish_need_battles(battle);
			newTask.setTaskType(MmochatTaskType.挑战地图守护神);
			MmochatMonsterFactory bigMonster = new MmochatMonsterFactory();
			bigMonster.setName(map.getMapName() + "守护神");
			bigMonster.setLevel(guiderInfo.getMyLevel());
			bigMonster.setBmpType(MmochatRoleBmpType.阐教女图);
			bigMonster.setDao(MmochatBattleService.getStandardDao(guiderInfo
					.getMyLevel(), 0));
			bigMonster.setLevelType(MmochatBossLevelType.自身等级);

			MmochatAttackModeOfNormal attackMode = new MmochatAttackModeOfNormal();
			bigMonster.setEquipLevel(1);
			bigMonster.setTeamNum(2);
			bigMonster.setKillRound(1);
			bigMonster.setPhysical(0.4);
			bigMonster.setMagic(0.2);
			bigMonster.setBossMaxSkillTargetNum(2);
			bigMonster.setSpeedPointAddType(1);

			MmochatMonster monsterValue = MmochatEquipService
					.setMonsterBattleParam(bigMonster);
			bigMonster = (MmochatMonsterFactory) monsterValue;

			attackMode.setPhysicalAttackOpportunity((int) (64));
			attackMode.setMagicAttackOppotunity((int) (16));
			attackMode.setLimitOppotunity((int) (20));

			bigMonster.setAttackMode_normal(attackMode);

			List<MmochatMonsterFactory> smallMonster = new ArrayList<MmochatMonsterFactory>();
			for (int i1 = 0; i1 < 5; i1++) {
				if (i1 == 2) {
					continue;
				}
				MmochatMonsterFactory monster = new MmochatMonsterFactory();
				monster.setName(map.getMapName() + "守护神");
				monster.setLevel(guiderInfo.getMyLevel());
				monster.setBmpType(bmpList[i1]);
				monster.setDao(MmochatBattleService.getStandardDao(guiderInfo
						.getMyLevel(), 0));
				monster.setLevelType(MmochatBossLevelType.自身等级);

				MmochatAttackModeOfNormal attackMode1 = new MmochatAttackModeOfNormal();
				monster.setEquipLevel(1);
				monster.setTeamNum(2);
				monster.setKillRound(1);
				monster.setPhysical(0.4);
				monster.setMagic(0.2);
				monster.setBossMaxSkillTargetNum(2);
				monster.setSpeedPointAddType(1);

				monsterValue = MmochatEquipService
						.setMonsterBattleParam(monster);
				monster = (MmochatMonsterFactory) monsterValue;

				attackMode1.setPhysicalAttackOpportunity((int) (64));
				attackMode1.setMagicAttackOppotunity((int) (16));
				attackMode1.setLimitOppotunity((int) (20));
				monster.setAttackMode_normal(attackMode1);
				smallMonster.add(monster);
			}
			for (int i1 = 0; i1 < 5; i1++) {
				MmochatMonsterFactory monster = new MmochatMonsterFactory();
				monster.setName("宠物");
				monster.setLevel(guiderInfo.getMyLevel());
				monster.setBmpType(MmochatRoleBmpType.玄武);
				monster.setDao(MmochatBattleService.getStandardDao(guiderInfo
						.getMyLevel(), 0));
				monster.setLevelType(MmochatBossLevelType.自身等级);

				MmochatAttackModeOfNormal attackMode1 = new MmochatAttackModeOfNormal();
				monster.setEquipLevel(1);
				monster.setTeamNum(2);
				monster.setKillRound(1);
				monster.setPhysical(0.4);
				monster.setMagic(0.2);
				monster.setBossMaxSkillTargetNum(2);
				monster.setSpeedPointAddType(1);

				monsterValue = MmochatEquipService
						.setMonsterBattleParam(monster);
				monster = (MmochatMonsterFactory) monsterValue;

				attackMode1.setPhysicalAttackOpportunity((int) (80));
				attackMode1.setMagicAttackOppotunity((int) (20));
				attackMode1.setLimitOppotunity((int) (0));
				monster.setAttackMode_normal(attackMode1);
				smallMonster.add(monster);
			}
			battle.setBoss(bigMonster);
			battle.setSmallBoss(smallMonster);
		}
		MmochatMapGuiderData data = new MmochatMapGuiderData();
		data.setNpcList(npcList);
		guiderPositions.put(mapId, data);
	}

	// 设置新的守护神
	public static void setMapGuiderData(MmochatMap map, List<Integer> roleList,
			List<MmochatTempNpc> npcList) {
		if (MmochatGmService.isSystemExit()) {
			return;
		}
		if (guiderPositions == null) {
			return;
		}
		if (map != null && npcList != null && npcList.size() == 5) {
			MmochatMapGuiderData oldData = null;

			synchronized (guiderPositions) {
				oldData = guiderPositions.get(map.getMapId());
				MmochatMapGuiderData newData = new MmochatMapGuiderData();
				newData.setNpcList(npcList);
				newData.setRoleList(roleList);
				guiderPositions.put(map.getMapId(), newData);
				// 从地图上删除旧的NPC
				if (oldData != null) {
					for (MmochatTempNpc npc : oldData.getNpcList()) {
						map.delNpc(npc);
						MmochatNpcService.npcs.remove(npc.getNpcId());
					}
				}

				// 增加新的NPC到地图上
				for (MmochatTempNpc npc : npcList) {
					map.addNpc(npc);
					MmochatNpcService.npcs.put(npc.getNpcId(), npc);
				}
			}
			MmochatChenHaoType chenhaoType = MmochatChenHaoType.valueOf(map
					.getMapName()
					+ "守护神");

			if (oldData != null) {
				if (oldData.isOnDudy() && oldData.getRoleList().size() > 0) {
					int addDao = oldData.getDao();
					long id = System.currentTimeMillis();
					for (Integer roleId : oldData.getRoleList()) {
						// 如果旧的守护神存在，则结算道行奖励，并通知用户
						MmochatPlayer me = MmochatGmService
								.getRoleInfoById(roleId);
						if (me != null) {
							//刷道比赛
							MmochatPropService.dealDaoActivity(me, addDao);
							
							int dao = MmochatBattleService.getRealDao(me,
									addDao);
							me.addDao(dao);
							String msg;
							if (dao > 0) {
								msg = "您的"
										+ MmochatUtil.wrapColor(map
												.getMapName()
												+ "守护神", Color.yellow)
										+ "被其它玩家打下来了，您在位期间共获得了" + dao
										+ "天道行(削减前"+addDao+"天)奖励。";
							} else {
								msg = "您的"
										+ MmochatUtil.wrapColor(map
												.getMapName()
												+ "守护神", Color.yellow)
										+ "被其它玩家打下来了，" + "在此之前没有其它玩家挑战您，"
										+ "故没有额外道行奖励!";
							}

							MmochatGmService.sendKufuPrivateMsg(roleId, msg,
									false, id);
							id++;
							// 称号扣除
							if (chenhaoType != null) {
								me.delAndUpdateChenHao(chenhaoType);
							}

							try {
								MmochatDao.addRoleDaoAndUpdateChenhao(roleId,
										dao, me.getGson_chenhao());
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
				}
				oldData.setOnDudy(false);
				oldData.getNpcList().clear();
				oldData.getRoleList().clear();
			}

			// 增加守护神称号
			if (chenhaoType != null) {
				for (Integer roleId : roleList) {
					MmochatPlayer me = MmochatGmService.getRoleInfoById(roleId);
					// 称号增加
					if (chenhaoType != null) {
						MmochatChenHao chenhao = new MmochatChenHao();
						chenhao.setType(chenhaoType);
						MmochatEquipAttribute attr = new MmochatEquipAttribute();
						attr.setType(MmochatEquipAttributeType.全属性);
						attr.setValue(5);
						chenhao.addAttr(attr);
						me.addAndUpdateChenHao(chenhao);

						try {
							MmochatDao.saveChenhao(me);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			}

		}
	}

	// 设置默认的门派王者数据
	public static void setDefaultFamilyGuiderData(MmochatMap map) {
		if (map == null) {
			return;
		}
		int mapId = map.getMapId();
		MmochatMapGuider guiderInfo = map.getMapGuiderInfo();
		if (guiderInfo == null) {
			return;
		}

		MmochatRoleBmpType bmpType;
		switch (map.getMapId()) {
		case MmochatConstant.mapId_人教:
			bmpType = MmochatRoleBmpType.人教女图;
			break;
		case MmochatConstant.mapId_阐教:
			bmpType = MmochatRoleBmpType.阐教女图;
			break;
		case MmochatConstant.mapId_截教:
			bmpType = MmochatRoleBmpType.截教女图;
			break;
		default:
			bmpType = MmochatRoleBmpType.人教女图;
			break;
		}

		List<MmochatTempNpc> npcList = new ArrayList<MmochatTempNpc>();

		MmochatTempNpc boss = new MmochatTempNpc(true);
		MmochatNpcService.npcs.put(boss.getNpcId(), boss);
		boss.getFunctionItems().add(MmochatNpcItemType.挑战本派最强);
		boss.getFunctionItems().add(MmochatNpcItemType.挑战说明);
		boss.getFunctionItems().add(MmochatNpcItemType.挑战经验锁妖塔);
		boss.getFunctionItems().add(MmochatNpcItemType.挑战道行锁妖塔);
		boss.getFunctionItems().add(MmochatNpcItemType.锁妖塔说明);
		boss.setLevel(guiderInfo.getMyLevel());
		boss.setLifeTime(MmochatConstant.MS_PER_DAY * 3650);
		boss.setName(map.getMapName() + "最强");
		boss.setBmpType(bmpType);
		boss.setMapId(mapId);
		boss.setX((short) guiderInfo.getX());
		boss.setY((short) guiderInfo.getY());
		boss.setFace(guiderInfo.getFace());

		map.addNpc(boss);
		npcList.add(boss);

		MmochatTask newTask = new MmochatTask();
		newTask.setTaskId(0);
		boss.addTask(newTask);
		MmochatNpcBattle battle = new MmochatNpcBattle();
		newTask.setFinish_need_battles(battle);
		newTask.setTaskType(MmochatTaskType.挑战本派最强);
		MmochatMonsterFactory bigMonster = new MmochatMonsterFactory();
		bigMonster.setName(map.getMapName() + "最强");
		bigMonster.setLevel(guiderInfo.getMyLevel());
		bigMonster.setBmpType(bmpType);
		bigMonster.setDao(MmochatBattleService.getStandardDao(guiderInfo
				.getMyLevel(), 0));
		bigMonster.setLevelType(MmochatBossLevelType.自身等级);

		MmochatAttackModeOfNormal attackMode = new MmochatAttackModeOfNormal();
		bigMonster.setEquipLevel(1);
		bigMonster.setTeamNum(2);
		bigMonster.setKillRound(1);
		bigMonster.setPhysical(0.4);
		bigMonster.setMagic(0.2);
		bigMonster.setBossMaxSkillTargetNum(2);
		bigMonster.setSpeedPointAddType(1);

		MmochatMonster monsterValue = MmochatEquipService
				.setMonsterBattleParam(bigMonster);
		bigMonster = (MmochatMonsterFactory) monsterValue;

		attackMode.setPhysicalAttackOpportunity((int) (64));
		attackMode.setMagicAttackOppotunity((int) (16));
		attackMode.setLimitOppotunity((int) (20));

		bigMonster.setAttackMode_normal(attackMode);

		List<MmochatMonsterFactory> smallMonster = new ArrayList<MmochatMonsterFactory>();

		MmochatMonsterFactory monster = new MmochatMonsterFactory();
		monster.setName("宠物");
		monster.setLevel(guiderInfo.getMyLevel());
		monster.setBmpType(MmochatRoleBmpType.玄武);
		monster.setDao(MmochatBattleService.getStandardDao(guiderInfo
				.getMyLevel(), 0));
		monster.setLevelType(MmochatBossLevelType.自身等级);

		MmochatAttackModeOfNormal attackMode1 = new MmochatAttackModeOfNormal();
		monster.setEquipLevel(1);
		monster.setTeamNum(2);
		monster.setKillRound(1);
		monster.setPhysical(0.4);
		monster.setMagic(0.2);
		monster.setBossMaxSkillTargetNum(2);
		monster.setSpeedPointAddType(1);

		monsterValue = MmochatEquipService.setMonsterBattleParam(monster);
		monster = (MmochatMonsterFactory) monsterValue;

		attackMode1.setPhysicalAttackOpportunity((int) (80));
		attackMode1.setMagicAttackOppotunity((int) (20));
		attackMode1.setLimitOppotunity((int) (0));
		monster.setAttackMode_normal(attackMode1);
		smallMonster.add(monster);

		battle.setBoss(bigMonster);
		battle.setSmallBoss(smallMonster);

		MmochatMapGuiderData data = new MmochatMapGuiderData();
		data.setNpcList(npcList);
		familyGuiderPositions.put(mapId, data);
	}

	// 设置新的门派王者
	public static void setFamilyGuiderData(MmochatMap map,
			List<Integer> roleList, List<MmochatTempNpc> npcList) {
		if (MmochatGmService.isSystemExit()) {
			return;
		}
		if (familyGuiderPositions == null) {
			return;
		}
		if (map != null && npcList != null && npcList.size() == 1) {
			MmochatMapGuiderData oldData = null;

			synchronized (familyGuiderPositions) {
				oldData = familyGuiderPositions.get(map.getMapId());
				MmochatMapGuiderData newData = new MmochatMapGuiderData();
				newData.setNpcList(npcList);
				newData.setRoleList(roleList);
				familyGuiderPositions.put(map.getMapId(), newData);
				// 从地图上删除旧的NPC
				if (oldData != null) {
					for (MmochatTempNpc npc : oldData.getNpcList()) {
						map.delNpc(npc);
						MmochatNpcService.npcs.remove(npc.getNpcId());
					}
				}

				// 增加新的NPC到地图上
				for (MmochatTempNpc npc : npcList) {
					map.addNpc(npc);
					MmochatNpcService.npcs.put(npc.getNpcId(), npc);
				}
			}
			MmochatChenHaoType chenhaoType = MmochatChenHaoType.valueOf(map
					.getMapName()
					+ "最强");

			if (oldData != null) {
				if (oldData.isOnDudy() && oldData.getRoleList().size() > 0) {
					int addDao = oldData.getDao();
					long id = System.currentTimeMillis();
					for (Integer roleId : oldData.getRoleList()) {
						// 如果旧的王者存在，则结算道行奖励，并通知用户
						MmochatPlayer me = MmochatGmService
								.getRoleInfoById(roleId);
						if (me != null) {
							//刷道比赛
							MmochatPropService.dealDaoActivity(me, addDao);
							
							int dao = MmochatBattleService.getRealDao(me,
									addDao);
							me.addDao(dao);
							String msg;
							if (dao > 0) {
								msg = "您的"
										+ MmochatUtil.wrapColor(map
												.getMapName()
												+ "最强地位", Color.yellow)
										+ "被其它玩家打下来了，您在位期间共获得了" + dao
										+ "天道行(削减前"+addDao+"天)奖励。";
							} else {
								msg = "您的"
										+ MmochatUtil.wrapColor(map
												.getMapName()
												+ "最强地位", Color.yellow)
										+ "被其它玩家打下来了，" + "在此之前没有其它玩家挑战您，"
										+ "故没有额外道行奖励!";
							}

							MmochatGmService.sendKufuPrivateMsg(roleId, msg,
									false, id);
							id++;
							// 称号扣除
							if (chenhaoType != null) {
								me.delAndUpdateChenHao(chenhaoType);
							}

							try {
								MmochatDao.addRoleDaoAndUpdateChenhao(roleId,
										dao, me.getGson_chenhao());
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
				}
				oldData.setOnDudy(false);
				oldData.getNpcList().clear();
				oldData.getRoleList().clear();
			}

			// 增加门派最强称号
			if (chenhaoType != null) {
				for (Integer roleId : roleList) {
					MmochatPlayer me = MmochatGmService.getRoleInfoById(roleId);
					// 称号增加
					if (chenhaoType != null) {
						MmochatChenHao chenhao = new MmochatChenHao();
						chenhao.setType(chenhaoType);
						MmochatEquipAttribute attr = new MmochatEquipAttribute();
						attr.setType(MmochatEquipAttributeType.全异常抵抗);
						attr.setValue(20);
						chenhao.addAttr(attr);

						attr = new MmochatEquipAttribute();
						attr.setType(MmochatEquipAttributeType.忽视全异常抵抗);
						attr.setValue(20);
						chenhao.addAttr(attr);
						me.addAndUpdateChenHao(chenhao);

						try {
							MmochatDao.saveChenhao(me);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			}

		}
	}
}
